package jinyilw.swing.i18n;

import jinyilw.swing.FontTools;
import jinyilw.swing.FontTools.CustomFont;
import jinyilw.swing.FontTools.UIFont;
import jinyilw.swing.SwingTools;
import jinyilw.swing.visuals.Jcomp.IJMouseActComp;
import jinyilw.swing.visuals.comp.IMouseActComp;
import jinyilw.swing.visuals.comp.RichTitleComp;

import javax.swing.AbstractButton;
import javax.swing.JLabel;
import java.awt.Component;
import java.awt.Font;
import java.util.ArrayList;

public class ICSetting
{
	private final String key;
	private final String zhString;
	private final String enString;
	// private Component c;
	private String currentString, languageTag;
	private boolean zoomOutEn, enSetted;
	private Font zNormalFont, zEnglishFont;
	private Font normalFont, englishFont;
	private Object[] replace;

	public ICSetting(String key, String zhString, String enString)
	{
		this.key = key;
		this.zhString = zhString;
		this.enString = enString;
	}

	public ICSetting(String key, String zhString, String enString,
			boolean autoWrap)
	{
		this.key = key;
		if (autoWrap)
		{
			this.zhString = SwingTools.getHtmlText(zhString);
			this.enString = SwingTools.getHtmlText(enString);
		} else
		{
			this.zhString = zhString;
			this.enString = enString;
		}

	}

	public ICSetting(String key, String zhString, String enString,
			Font normalFont, Font smallFont)
	{
		this.key = key;
		this.zhString = zhString;
		this.enString = enString;
		setFont(normalFont, smallFont);
	}

	public ICSetting(String key, String zhString, String enString, Component c)
	{
		this.key = key;
		this.zhString = zhString;
		this.enString = enString;
		setZoomOutEN(c, 1);
	}

	public ICSetting(String key, String zhString, String enString, Component c,
			int d)
	{
		this.key = key;
		this.zhString = zhString;
		this.enString = enString;
		setZoomOutEN(c, d);
	}

	public void setZoomOutEN(Component c)
	{
		setZoomOutEN(c, 1);
	}

	public void setZoomOutEN(Component c, int d)
	{
		Font font;
		if (c != null && (font = c.getFont()) != null)
		{
			zNormalFont = font;
			int size = font.getSize() - d;
			if (FontTools.useSystemFontForEnglish)
				zEnglishFont = FontTools.getSystemFont(size);
			else
				zEnglishFont = zNormalFont.deriveFont((float) size);
			zoomOutEn = true;
		}
	}

	public void setFont(Font normalFont, Font smallFont)
	{
		if (normalFont == null || smallFont == null)
			return;
		this.zNormalFont = normalFont;
		this.zEnglishFont = smallFont;
		zoomOutEn = true;
	}

	public void changStyleFont(CustomFont[] customFonts)
	{
		if (zoomOutEn)
		{
			if (zNormalFont instanceof CustomFont)
				zNormalFont = customFonts[zNormalFont.getSize()]
						.getStyleFont(zNormalFont.getStyle());
			if (zEnglishFont instanceof CustomFont)
				zEnglishFont = customFonts[zEnglishFont.getSize()]
						.getStyleFont(zEnglishFont.getStyle());
		}
		if (enSetted)
		{
			if (normalFont instanceof CustomFont)
				normalFont = customFonts[normalFont.getSize()]
						.getStyleFont(normalFont.getStyle());
			if (englishFont instanceof CustomFont)
				englishFont = customFonts[englishFont.getSize()]
						.getStyleFont(englishFont.getStyle());
		}
	}

	// 替换%d符号
	public ICSetting setReplaceData(Object... replaceDatas)
	{
		this.replace = replaceDatas;
		return this;
	}

	public void setReplaceData(Component c, Object... replaceDatas)
	{
		this.replace = replaceDatas;
		updateComponentText(c);
	}

	public void setReplaceData(LanguageChanger changer, Object... replaceDatas)
	{
		this.replace = replaceDatas;
		updateText(changer);
	}

	public void updateComponentText(Component c)
	{
		if (c instanceof AbstractButton)
			((AbstractButton) c).setText(getI18nString());
		else if (c instanceof JLabel)
			((JLabel) c).setText(getI18nString());
		else if (c instanceof IJMouseActComp)
			((IJMouseActComp) c).updateText(getI18nString());
		else if (c instanceof RichTitleComp)
			((RichTitleComp) c).updateText(getI18nString());
		else if (c instanceof IMouseActComp)
			((IMouseActComp) c).updateText(getI18nString());
		if (isFontChange())
			c.setFont(getFont(c));
	}

	public void changComponentText(Component c)
	{
		if (needChange())
			updateComponentText(c);
	}

	public void changText(LanguageChanger changer)
	{
		if (needChange())
			updateText(changer);
	}

	protected void updateText(LanguageChanger changer)
	{
		changer.updateText(getI18nString());
		if (isFontChange() && changer instanceof Component)
		{
			Component c = (Component) changer;
			Font font = c.getFont();
			Font newFont = getFont(font);
			if (newFont != font)
				c.setFont(newFont);
		}
	}

	public String getI18nString()
	{
		if (LanguageTools.languageSettingEnable)
		{
			if (languageTag != LanguageTools.languageTag)
			{
				languageTag = LanguageTools.languageTag;
				currentString = LanguageTools.getText(key, zhString, enString);
			}
		} else
			currentString = zhString;
		if (replace != null)
			return getReplaceString(currentString);
		return currentString;
	}

	private String getReplaceString(String string)
	{
		return String.format(string, replace);
	}

	public boolean isFontChange()
	{
		return (zoomOutEn || FontTools.useSystemFontForEnglish)
				&& LanguageTools.languageSettingEnable;
	}

	public boolean needChange()
	{
		return languageTag != LanguageTools.languageTag
				&& LanguageTools.languageSettingEnable;
	}

	public Font getFont(Component c)
	{
		return getFont(c.getFont());
	}

	public Font getFont(Font font)
	{
		if (zoomOutEn)
			return LanguageTools.isEnglish() ? zEnglishFont : zNormalFont;
		if (!(font instanceof UIFont))
			return font;
		if (enSetted && font.getSize() == normalFont.getSize())
			return LanguageTools.isEnglish() ? englishFont : normalFont;
		normalFont = font;
		englishFont = FontTools.getSystemUIFont(font.getSize());
		enSetted = true;
		return LanguageTools.isEnglish() ? englishFont : normalFont;
	}

	@Override
	public String toString()
	{
		return getI18nString();
	}

	public static ArrayList<ICSetting> createArray(String key, String zhString,
			String enString, int start, int end)
	{
		ArrayList<ICSetting> list = new ArrayList<>();
		for (int i = start; i <= end; i++)
		{
			list.add(new ICSetting(key + i, zhString + i, enString + i));
		}
		return list;
	}
}
